﻿using ProductMonitor.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ProductMonitor.UserControls
{
    /// <summary>
    /// 自定义控件雷达图
    /// </summary>
    public partial class RadarUC : UserControl
    {
        public RadarUC()
        {
            InitializeComponent();

            //窗体变化事件
            SizeChanged += OnSizeChanged;
        }

        //窗体大小发生变化--重新绘图
        private void OnSizeChanged(object sender, SizeChangedEventArgs e)
        {
            DiaC();
        }
        

        /// <summary>
        /// 雷达图数据属性（使用依赖属性--便于绑定）
        /// </summary>
        public List<RadarModel> ItemSource
        {
            get { return (List<RadarModel>)GetValue(ItemSourceProperty); }
            set { SetValue(ItemSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ItemSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemSourceProperty =
            DependencyProperty.Register("ItemSource", typeof(List<RadarModel>), typeof(RadarUC));

        //窗体大小发生变化的函数
        private void DiaC()
        {
            //避免空数据源
            if (ItemSource == null || ItemSource.Count == 0)
                return;

            //清除
            mainCanvas.Children.Clear();
            P1.Points.Clear();
            P2.Points.Clear();
            P3.Points.Clear();
            P4.Points.Clear();
            P5.Points.Clear();

            //调整窗体大小以及参数
            double size = Math.Min(RenderSize.Width, RenderSize.Height);//依据窗体渲染器（Render-->groupBox）的大小调整长宽，使其能够成为一个正方形

            layoutGrid.Height = size;
            layoutGrid.Width = size;

            //半径
            double radius = size / 2;
            //步长
            double step = 360.0 / ItemSource.Count;


            //添加新的点
            for (int i = 0; i < ItemSource.Count; i++)
            {
                //偏移量参数
                double x_hat = (radius - 20) * Math.Cos((step * i - 90) * Math.PI / 180);
                double y_hat = (radius - 20) * Math.Sin((step * i - 90) * Math.PI / 180);

                //x y坐标
                P1.Points.Add(new Point(radius + x_hat, radius + y_hat));
                P2.Points.Add(new Point(radius + x_hat * 0.75, radius + y_hat * 0.75));//随系数百分比变化，由内至外
                P3.Points.Add(new Point(radius + x_hat * 0.5, radius + y_hat * 0.5));
                P4.Points.Add(new Point(radius + x_hat * 0.25, radius + y_hat * 0.25));

                //多边形区域
                P5.Points.Add(new Point(radius + x_hat * ItemSource[i].Value * 0.01, radius + y_hat * ItemSource[i].Value * 0.01));

                //绘制标签
                TextBlock txt = new TextBlock();
                txt.Width = 60;
                txt.FontSize = 10;
                txt.TextAlignment = TextAlignment.Center;
                txt.Text=ItemSource[i].ItemName;
                txt.Foreground = new SolidColorBrush(Color.FromArgb(100,255,255,255));

                //固定与画布的相对位置
                txt.SetValue(Canvas.LeftProperty,radius+ (radius - 10) * Math.Cos((step * i - 90) * Math.PI / 180)-30);
                txt.SetValue(Canvas.TopProperty,radius+ (radius - 10) * Math.Sin((step * i - 90) * Math.PI / 180)-7);

                mainCanvas.Children.Add(txt);
            }

        }

    }
}
